package week.six;

import javax.microedition.khronos.egl.*;
import javax.microedition.khronos.opengles.*;

import android.opengl.*;
import android.opengl.GLSurfaceView.Renderer;

public class Display implements Renderer {

	private Triangle triangle;
	private Square square;
	private Vec2 touchPos;
//	private long prevTime = 0;
	private float rotation = 0;
	
	public Display() {
		triangle = new Triangle();
		square = new Square(new Vec2(200,200),200,200);
		touchPos = new Vec2(300,200);
//		box = new Box(new Vec3(100,100,100),new Vec3(200,200,0),new Vec3(0,30,30));
//		cube = new Cube();
	}

	public void onDrawFrame(GL10 gl) {
		//long time = System.currentTimeMillis();
		//float deltaTime = (float)(time - prevTime) / 1000.0f;
		
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		//gl.glTranslatef(touchPos.getX()-100, touchPos.getY()-100, 0);
		gl.glRotatef(rotation, 0, 0, 1);
		triangle.draw(gl);
		gl.glLoadIdentity();
		gl.glTranslatef(touchPos.x-100, touchPos.y+100, 0);
		square.position = touchPos;
		
		
		gl.glRotatef(rotation, 0, 0, 1);
		square.draw(gl);
		
		rotation += 1;
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glShadeModel(GL10.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		if (height == 0)
			height = 1;
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluOrtho2D(gl, 0, width, height, 0);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}
	
	public void setTouchPos(Vec2 touchPos) {
		this.touchPos = touchPos;
	}
	
}
